<?php
/***
* @version $Id: functions_selects.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2005 - 2006 aboyd (Tony Boyd) http://www.outshine.com/forums/
*            (c) 2005 - 2006 pichirichi <pichirichi@pichirichi.com> http://www.pichirichi.com
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

if ( !defined('IN_R3BORN') )
{
	die('Hacking attempt');
}

//
// Pick a language, any language ...
//
function language_select($default, $select_name = "language", $dirname="language")
{
	global $phpEx, $root_path;

	$dir = opendir($root_path . $dirname);

	$lang = array();
	while ( $file = readdir($dir) )
	{
		if (preg_match('#^lang_#i', $file) && !is_file(@realpath($root_path . $dirname . '/' . $file)) && !is_link(@realpath($root_path . $dirname . '/' . $file)))
		{
			$filename = trim(str_replace("lang_", "", $file));
			$displayname = preg_replace("/^(.*?)_(.*)$/", "\\1 [ \\2 ]", $filename);
			$displayname = preg_replace("/\[(.*?)_(.*)\]/", "[ \\1 - \\2 ]", $displayname);
			$lang[$displayname] = $filename;
		}
	}

	closedir($dir);

	@asort($lang);
	@reset($lang);

	$lang_select = '<select name="' . $select_name . '">';
	while ( list($displayname, $filename) = @each($lang) )
	{
		$selected = ( strtolower($default) == strtolower($filename) ) ? ' selected="selected"' : '';
		$lang_select .= '<option value="' . $filename . '"' . $selected . '>' . ucwords($displayname) . '</option>';
	}
	$lang_select .= '</select>';

	return $lang_select;
}

//
// Pick a template/theme combo,
//
function style_select($default_style, $select_name = "style", $dirname = "templates")
{
	global $db;

	$sql = "SELECT themes_id, style_name
		FROM " . THEMES_TABLE . "
		ORDER BY template_name, themes_id";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, "Couldn't query themes table", "", __LINE__, __FILE__, $sql);
	}

	$style_select = '<select name="' . $select_name . '">';
	while ( $row = $db->sql_fetchrow($result) )
	{
		$selected = ( $row['themes_id'] == $default_style ) ? ' selected="selected"' : '';

		$style_select .= '<option value="' . $row['themes_id'] . '"' . $selected . '>' . $row['style_name'] . '</option>';
	}
	$style_select .= "</select>";

	return $style_select;
}

//
// Pick a timezone
//
function tz_select($default, $select_name = 'timezone')
{
	global $sys_timezone, $lang;

	if ( !isset($default) )
	{
		$default == $sys_timezone;
	}
	$tz_select = '<select name="' . $select_name . '">';

	while( list($offset, $zone) = @each($lang['tz']) )
	{
		$selected = ( $offset == $default ) ? ' selected="selected"' : '';
		$tz_select .= '<option value="' . $offset . '"' . $selected . '>' . $zone . '</option>';
	}
	$tz_select .= '</select>';

	return $tz_select;
}

//
// Visual pick Date Format for non technical users
//
function date_select($default_format, $select_name = 'dateformat')
{
	global $lang,$config;

	//---------------------------------------------------
	$date_format_list[] = array('F d Y');
	$date_format_list[] = array('F d Y, H:i');
	$date_format_list[] = array('F d Y, G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('d F Y');
	$date_format_list[] = array('d F Y, H:i');
	$date_format_list[] = array('d F Y, G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('l, d F Y');
	$date_format_list[] = array('l, d F Y, H:i');
	$date_format_list[] = array('l, d F Y, G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('D, M d Y');
	$date_format_list[] = array('D, M d Y, H:i');
	$date_format_list[] = array('D, M d Y, G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('D d M');
	$date_format_list[] = array('D d M, Y H:i');
	$date_format_list[] = array('D d M, Y G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('d/m/Y');
	$date_format_list[] = array('d/m/Y H:i');
	$date_format_list[] = array('d/m/Y G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('m/d/Y');
	$date_format_list[] = array('m/d/Y H:i');
	$date_format_list[] = array('m/d/Y G:i A');
	//---------------------------------------------------
	$date_format_list[] = array('m.d.Y');
	$date_format_list[] = array('m.d.Y H:i');
	$date_format_list[] = array('m.d.Y G:i A');
	//---------------------------------------------------


	//---------------------------------------------------
	// Set a default value.
	//---------------------------------------------------
	if ( empty($default_format) )
	{
		$default_format = $date_format_list[11][0];
	}


	$date_select = '<select name="' . $select_name . '">'."\n";
	for($i = 0; $i < count($date_format_list); $i++)
	{
		$date_format = $date_format_list[$i][0];
		$date_desc   = create_date($date_format_list[$i][0],time(),$config['board_timezone']);

		$selected = ( $date_format == $default_format ) ? ' selected="selected"' : '';
		$date_select .= '<option value="' . $date_format . '"' . $selected . '>' . $date_desc . '</option>'."\n";

		$counter = 0;
	}
	$date_select .= '</select>'."\n";

	return $date_select;
}

//
// $selected = the current auth type
// $select_name = the name of the parameter to be use in the <form>
// $show_owner = an optional parameter to trigger if the content can has special owner only access.
//
function user_auth_select($selected, $select_name, $show_owner = false)
{
	global $lang;

	if ($show_owner)
	{
		$auth_types = array(
			'0' => $lang['All'],
			'1' => $lang['Guests'],
			'2' => $lang['Reg'],
			'3' => $lang['Owner'],
			'4' => $lang['Admins']
		);
	}
	else
	{
		$auth_types = array(
			'0' => $lang['All'],
			'1' => $lang['Guests'],
			'2' => $lang['Reg'],
			'4' => $lang['Admins']
		);
	}

	$auth_select = '<select id="' . $select_name . '" name="' . $select_name . '">';

	for ($i = 0; $i < count($auth_types); $i++)
	{
		// Skip array item 3 if $show_owner is not true
		if (!$show_owner && $i == 3)
		{
			$i++;
		}

		$select = ( $selected == $i ) ? ' selected="selected"' : '';
		$auth_select .= '<option value="' . $i . '"' . $select . '>' . $auth_types[$i] . '</option>';

	}
	$auth_select .= '</select>';

	return $auth_select;
}

//
// Groups to match is an array of group ids.
//
function group_auth_select($groups_to_match = array())
{
	global $db, $lang;

	$sql = "SELECT group_id, group_name FROM " . GROUPS_TABLE . " WHERE group_single_user = 0 ORDER BY group_id";
	if( !($result = $db->sql_query($sql)) )
	{
		message_die(CRITICAL_ERROR, 'Could not query user groups information', '', __LINE__, __FILE__, $sql);
	}

	$grouplist = '';
	while ($row = $db->sql_fetchrow($result))
	{
		$checked = (in_array($row['group_id'], $groups_to_match)) ? 'checked="checked"' : '';
		$grouplist .= '<input type="checkbox" name="group' . strval($row['group_id']) . '" ' . $checked . ' />' . $row['group_name'] . '<br />';
	}

	if(empty($grouplist))
	{
		$grouplist = '&nbsp;&nbsp;' . $lang['None'];
	}

	return $grouplist;
}

?>